#include "stdafx.h"
#include <XSlider.h>

void XSlider::_loadVertices()
{
	if(Render != NULL)
	{
		XVertex* vert = new XVertex[4];

		vert[0].X = Rectangle.X;
		vert[0].Y = Rectangle.Y;
		vert[1].X = Rectangle.X + Rectangle.W;
		vert[1].Y = Rectangle.Y;
		vert[2].X = Rectangle.X; 
		vert[2].Y = Rectangle.Y + Rectangle.H;
		vert[3].X = Rectangle.X + Rectangle.W;
		vert[3].Y = Rectangle.Y + Rectangle.H;

		Render->SetVertex(vert, 4);
		delete[] vert;
	}
}

void XSlider::_loadTextureVertices()
{
	if(Render != NULL)
	{
		if(Vertical)
		{
			XVertex* vert = new XVertex[4];

			vert[0].X = (float)675/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)51/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)675/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)44/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)876/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)51/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)876/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)44/ParentWindow->GetTexture()->GetHeight();

			Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
			delete[] vert;
		}
		else
		{
			XVertex* vert = new XVertex[4];

			vert[0].X = (float)675/ParentWindow->GetTexture()->GetWidth();
			vert[0].Y = (float)44/ParentWindow->GetTexture()->GetHeight();
			vert[1].X = (float)876/ParentWindow->GetTexture()->GetWidth();
			vert[1].Y = (float)44/ParentWindow->GetTexture()->GetHeight();
			vert[2].X = (float)675/ParentWindow->GetTexture()->GetWidth();
			vert[2].Y = (float)51/ParentWindow->GetTexture()->GetHeight();
			vert[3].X = (float)876/ParentWindow->GetTexture()->GetWidth();
			vert[3].Y = (float)51/ParentWindow->GetTexture()->GetHeight();

			Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
			delete[] vert;
		}
	}
}

void XSlider::_loadSlider()
{
	if(Vertical)
	{
		XVertex* vert = new XVertex[4];

		vert[0].X = 0;
		vert[0].Y = 0;
		vert[1].X = 20;
		vert[1].Y = 0;
		vert[2].X = 0; 
		vert[2].Y = 14;
		vert[3].X = 20;
		vert[3].Y = 14;

		Slider->SetVertex(vert, 4);

		vert[0].X = (float)640/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)90/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)654/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)90/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)640/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)108/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)654/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)108/ParentWindow->GetTexture()->GetHeight();

		Slider->SetTexture(ParentWindow->GetTexture(), vert, 4);
		delete[] vert;
	}
	else
	{
		XVertex* vert = new XVertex[4];

		vert[0].X = 0;
		vert[0].Y = 0;
		vert[1].X = 14;
		vert[1].Y = 0;
		vert[2].X = 0; 
		vert[2].Y = 20;
		vert[3].X = 14;
		vert[3].Y = 20;

		Slider->SetVertex(vert, 4);

		vert[0].X = (float)640/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)90/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)654/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)90/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)640/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)108/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)654/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)108/ParentWindow->GetTexture()->GetHeight();

		Slider->SetTexture(ParentWindow->GetTexture(), vert, 4);
		delete[] vert;
	}
}

XSlider::XSlider(XHash hash, XWindow* parentWindow)
	: XWidget(hash, parentWindow)
{
	Slider = new XRenderPack(Hash.String + "-Slider");
	
	Render = new XRenderPack(Hash.String + "-Layout");

	ParentWindow->RegisterControlsRender(Render);
	ParentWindow->RegisterControlsRender(Slider);

	Vertical = false;

	Rectangle.W = 200;
	Rectangle.H = 4;

	SetMaxValue(100);
	SetValue(0);

	Slided = false;

	_loadVertices();
	_loadTextureVertices();
	_loadSlider();
}

void XSlider::SetGeometry(XRectangle rect)
{
	Rectangle.X = rect.X;
	Rectangle.Y = rect.Y;

	if(Vertical)
	{
		Rectangle.W = 4;
		Rectangle.H = rect.H;
		SliderTranslation.Y = (Rectangle.Y + Rectangle.H) - ((float)Value/MaxValue * (Rectangle.H+10));
		SliderTranslation.X = Rectangle.X - 8;
	}
	else
	{
		Rectangle.W = rect.W;
		Rectangle.H = 4;
		SliderTranslation.X = Rectangle.X + (float)Value/MaxValue * Rectangle.W-7;
		SliderTranslation.Y = Rectangle.Y - 8;
	}

	_loadVertices();
}

void XSlider::SetVertical()
{
	Vertical = true;

	Rectangle.H = Rectangle.W;
	Rectangle.W = 4;

	SliderTranslation.Y = (Rectangle.Y + Rectangle.H) - ((float)Value/MaxValue * (Rectangle.H+10));
	SliderTranslation.X = Rectangle.X - 8;

	_loadVertices();
	_loadTextureVertices();
	_loadSlider();
}

void XSlider::SetHorizontal()
{
	Vertical = false;

	Rectangle.W = Rectangle.H;
	Rectangle.H = 4;

	SliderTranslation.X = Rectangle.X + (float)Value/MaxValue * Rectangle.W-7;
	SliderTranslation.Y = Rectangle.Y - 8;

	_loadVertices();
	_loadTextureVertices();
	_loadSlider();
}

int XSlider::GetValue()
{
	return Value;
}

void XSlider::SetValue(int value)
{
	Value = value;

	if(Value < 0)
		Value = 0;
	if(Value > MaxValue)
		Value = MaxValue;

	if(Vertical != true)
	{
		SliderTranslation.X = Rectangle.X + (float)Value/MaxValue * Rectangle.W-7;
		SliderTranslation.Y = Rectangle.Y - 8;
	}
	else
	{
		SliderTranslation.Y = (Rectangle.Y + Rectangle.H) - ((float)Value/MaxValue * (Rectangle.H+10));
		SliderTranslation.X = Rectangle.X - 8;
	}
}

void XSlider::SetMaxValue(int value)
{
	MaxValue = value;

	if(Vertical != true)
	{
		SliderTranslation.X = Rectangle.X + (float)Value/MaxValue * Rectangle.W-7;
		SliderTranslation.Y = Rectangle.Y - 8;
	}
	else
	{
		SliderTranslation.Y = (Rectangle.Y + Rectangle.H) - ((float)Value/MaxValue * (Rectangle.H+10));
		SliderTranslation.X = Rectangle.X - 8;
	}
}

void XSlider::SetDisable()
{
	Enabled = false;

	Slider->SetColor(XColor(255, 255, 255, 100));
	Render->SetColor(XColor(255, 255, 255, 100));
}

void XSlider::SetEnable()
{
	Enabled = true;

	Slider->SetColor(XColor(255, 255, 255, 255));
	Render->SetColor(XColor(255, 255, 255, 255));
}

void XSlider::Update(XInputModule* input)
{

	Slider->SetTranslation(XVector(SliderTranslation.X + ParentWindow->GetGeometry()->X, SliderTranslation.Y + ParentWindow->GetGeometry()->Y));
	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));

	XRectangle slider;

	if(Vertical)
		slider = XRectangle(Slider->GetTranslation().X, Slider->GetTranslation().Y, 20, 14);
	else
		slider = XRectangle(Slider->GetTranslation().X, Slider->GetTranslation().Y, 14, 20);

	XPosition pos = input->GetMouse().GetPosition();

	if(input->GetMouse().GetLeft().GetCurrent() && input->GetMouse().GetLeft().GetPrevious() != true)
	{
		if(pos.X > slider.X && pos.X < slider.X + slider.W && pos.Y > slider.Y && pos.Y < slider.Y + slider.H)
			Slided = true;
		else
			Slided = false;
	}
	else if(input->GetMouse().GetLeft().GetCurrent() != true)
		Slided = false;

	if(Slided)
	{
		if(Vertical)
			SetValue(MaxValue*(Rectangle.H-(pos.Y-Rectangle.Y-ParentWindow->GetGeometry()->Y))/Rectangle.H);
		else
			SetValue(MaxValue*(pos.X-Rectangle.X-ParentWindow->GetGeometry()->X)/Rectangle.W);
	}


}